import 'package:flutter/material.dart';
import 'package:test_flutter/common/style/common_style.dart';
import 'package:test_flutter/page/dynamic_page.dart';

/// 用户
class UserPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _UserPage();
  }
}

class _UserPage extends State<UserPage> {
  List<DynamicBean> _data = List.empty(growable: true);

  @override
  void initState() {
    super.initState();
    _getData();
  }

  @override
  Widget build(BuildContext context) {
    return NestedScrollView(
        headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
          return <Widget>[
            SliverAppBar(
              expandedHeight: 230.0,
              title: SizedBox(
                width: double.infinity,
                child: Container(
                  color: UIColors.common_color_black,
                  child: Column(
                    children: [
                      // 头像
                      Row(
                        children: [
                          ClipOval(
                            child: Image.network(
                              "https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=1998641779,182193269&fm=26&gp=0.jpg",
                              width: 80,
                              height: 80,
                            ),
                          ),
                          Column(
                            // 文字+图标
                            children: [
                              Text.rich(TextSpan(children: [
                                TextSpan(
                                    text: 'czhah',
                                    style: TextStyle(
                                        color: UIColors.common_color_white)),
                                WidgetSpan(child: Icon(Icons.lock_clock))
                              ])),
                              Text.rich(TextSpan(children: [
                                WidgetSpan(child: Icon(Icons.group)),
                                TextSpan(
                                    text: '目前什么都没有留下。',
                                    style: TextStyle(
                                        color: UIColors.common_color_white)),
                              ])),
                              Text.rich(TextSpan(children: [
                                WidgetSpan(
                                    child: Icon(Icons.location_on_sharp)),
                                TextSpan(
                                    text: '目前什么都没有留下。',
                                    style: TextStyle(
                                        color: UIColors.common_color_white)),
                              ]))
                            ],
                          )
                        ],
                      ),
                      // 链接
                      InkWell(
                        onTap: () {
                          print("点击链接");
                        },
                        child: SizedBox(
                          width: double.infinity,
                          child: Icon(Icons.link),
                        ),
                      ),
                      // 时间
                      Text(
                        '创建于：2015-05-25',
                        style: TextStyle(color: UIColors.common_color_white),
                      )
                      // 主题
                    ],
                  ),
                ),
              ),
            )
          ];
        },
        body: ListView.separated(
          itemBuilder: (context, index) {
            var dynamicBean = _data[index];
            String id = dynamicBean.id;
            String avatar = dynamicBean.actor.avatar_url;
            String name = dynamicBean.actor.login;
            var time = dynamicBean.created_at;
            String repoName = dynamicBean.repo.name;
            String tip = dynamicBean.comment.body;
            return Padding(
              padding: EdgeInsets.all(10),
              child: TextButton(
                onPressed: () => print("click $id"),
                style: ButtonStyle(
                  backgroundColor: MaterialStateProperty.all(UIColors.common_color_white),
                ),
                child: Container(
                  margin: EdgeInsets.only(top: 6, bottom: 6),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      Flex(
                        direction: Axis.horizontal,
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: <Widget>[
                          ClipOval(
                            child: Image.network(
                              avatar,
                              width: 32,
                              height: 32,
                            ),
                          ),
                          Expanded(
                            flex: 1,
                            child: Container(
                              margin: EdgeInsets.only(left: 4, right: 4),
                              child: Text(
                                '$name',
                                style: TextStyle(
                                    color: UIColors.common_color_black,
                                    fontWeight: FontWeight.bold,
                                    fontSize: 12),
                              ),
                            ),
                          ),
                          Text(
                            time,
                            style: TextStyle(
                                color: UIColors.common_color_black1,
                                fontSize: 14),
                          )
                        ],
                      ),
                      Padding(
                        padding: EdgeInsets.only(top: 10),
                        child: Text(
                          "$repoName",
                          style: TextStyle(
                              color: UIColors.common_color_black,
                              fontWeight: FontWeight.bold,
                              fontSize: 12),
                        ),
                      ),
                      Visibility(
                        /// 可以控制View显示与隐藏,Offstage也有此作用
                        visible: tip != null && tip.isNotEmpty,
                        child: Padding(
                          padding: EdgeInsets.only(top: 6),
                          child: Text(
                            '$tip',
                            style: TextStyle(
                                color: UIColors.common_color_black2,
                                fontSize: 14),
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
              ),
            );
          },
          itemCount: _data.length,
          separatorBuilder: (context, index) {
            /// 添加分割线
            return Container(
              width: double.infinity,
              height: 2,
            );
          },
        ));
  }

  _getData() {
    RepoBean repoBean = RepoBean();
    repoBean.name = 'CZGithubAppFlutter';
    repoBean.id = 123;
    ActorBean actorBean = ActorBean();
    actorBean.id = 987;
    actorBean.avatar_url =
        'https://dss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=1361850766,1428296927&fm=26&gp=0.jpg';
    actorBean.login = "czhah";
    CommentBean commentBean = CommentBean();
    commentBean.id = 123456;
    commentBean.node_id = "456789";
    commentBean.body = "树莓派4编译遇到同样的问题，不知该如何解决 ";
    DynamicBean dynamicBean = DynamicBean();
    dynamicBean.id = "123456";
    dynamicBean.created_at = "2020-05-29T03:14:05Z";
    dynamicBean.actor = actorBean;
    dynamicBean.repo = repoBean;
    dynamicBean.comment = commentBean;
    _data.add(dynamicBean);
    _data.add(dynamicBean);
    _data.add(dynamicBean);
    _data.add(dynamicBean);
    _data.add(dynamicBean);
    _data.add(dynamicBean);
    _data.add(dynamicBean);
  }
}

